#include "osDispatcher.h"
#include "Sch51.h"
#include "main.h"

extern sTask SCH_tasks_G[SCH_MAX_TASKS];
volatile unsigned long timestamp = 0;

void osDispatcherInit(void)
{
     LL_TIM_ClearFlag_UPDATE(TIM14);
     LL_TIM_EnableIT_UPDATE(TIM14);
     LL_TIM_EnableCounter(TIM14);
}
void osDispatcher(void)
{
    uint8_t Index;
    for (Index = 0; Index < SCH_MAX_TASKS; Index++)
    {
        // Check if there is a task at this location
        if (SCH_tasks_G[Index].pTask)
        {
            if (SCH_tasks_G[Index].Delay == 0)
            {
                // The task is due to run
                SCH_tasks_G[Index].RunMe += 1; // Inc. the 'RunMe' flag

                if (SCH_tasks_G[Index].Period)
                {
                    // Schedule regular tasks to run again
                    SCH_tasks_G[Index].Delay = SCH_tasks_G[Index].Period;
                }
            }
            else
            {
                // Not yet ready to run: just decrement the delay
                SCH_tasks_G[Index].Delay -= 1;
            }
        }
    }
    timestamp++;
}
